package com.bamboo.tool.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.bamboo.tool.entity.ApiClass;
import com.bamboo.tool.entity.ApiMethod;
import com.bamboo.tool.entity.ClassPersistenceInfo;
import com.bamboo.tool.entity.MethodPersistenceInfo;

import java.util.List;
import java.util.Map;

/**
 * @author bamboo
 * Date 2022/10/19 10:48
 * Description
 */
public class DescProcessUtil {

    public static String getClassDesc(ApiClass classz, List<String> allDescDort) {
        ClassPersistenceInfo persistence = classz.getPersistence();
        String rename = persistence.getRename();
        if (StringUtil.isNotBlank(rename)) {
            return rename;
        } else {
            String classDesc = DescProcessUtil.getDesc(allDescDort, classz.getDesc());
            return StrUtil.isEmpty(classDesc) ? classz.getClassName() : classDesc;
        }
    }

    public static String getMethodDesc(ApiMethod method, List<String> allDescDort) {
        MethodPersistenceInfo persistence = method.getPersistence();
        String rename = persistence.getRename();
        if (StringUtil.isNotBlank(rename)) {
            return rename;
        } else {
            String classDesc = DescProcessUtil.getDesc(allDescDort, method.getDesc());
            return StrUtil.isEmpty(classDesc) ? method.getMethodName() : classDesc;
        }
    }

    public static String getDesc(List<String> descFrames, Map<String, String> descMap) {
        if (CollUtil.isEmpty(descMap)) {
            return null;
        }
        for (String descFrame : descFrames) {
            String desc = descMap.get(descFrame);
            if (StrUtil.isNotBlank(desc)) {
                return desc;
            }
        }
        return null;
    }
}
